package com.bupt.secret.test;

import java.io.ByteArrayInputStream;
import java.security.Security;
import java.security.cert.CertificateFactory;


import java.util.Date;

import com.alibaba.fastjson2.util.DateUtils;
import org.apache.axis.encoding.Base64;
import org.bouncycastle.jce.provider.BouncyCastleProvider;

public class X509CertificateParser {

    public static void main(String[] args) {
        try {
            // 证书字符串，已经使用Base64编码
            String encryptCertStr = "MIICIDCCAcagAwIBAgIIFt42kOQN16QwCgYIKoEcz1UBg3UwIjELMAkGA1UEBhMC" +
                    "Q04xEzARBgNVBAMMClJvb3RDQV9TTTIwHhcNMjQwODMxMDE0MTI4WhcNMzQwODMx" +
                    "MDE0MTI4WjAdMQswCQYDVQQGEwJDTjEOMAwGA1UEAwwFY2VzaGkwWTATBgcqhkjO" +
                    "PQIBBggqgRzPVQGCLQNCAARhA/8hqtcztKTkUi+F+1G64l8NrWzGd9ppkChaMUTy" +
                    "ce4tMqhaEO0wo/Qind1ux2k+kwergtKUz6M73+63Xzvzo4HqMIHnMAkGA1UdEwQC" +
                    "MAAwCwYDVR0PBAQDAgO4MB0GA1UdDgQWBBTpPL7L6PeVlvlf8EzMXmDoYlH5FDAS" +
                    "BggqgRzQFAQBBQQGEwRudWxsMCkGA1UdJQQiMCAGCCsGAQUFBwMCBggrBgEFBQcD" +
                    "BAYKKwYBBAGCNxQCAjASBggqgRzQFAQBBAQGEwRudWxsMBIGCCqBHNAUBAEDBAYT" +
                    "BG51bGwwEgYIKoEc0BQEAQIEBhMEbnVsbDAfBgNVHSMEGDAWgBQ+ST5TZexLOO0S" +
                    "sFnXt4vfqEshTTASBggqgRzQFAQBAQQGEwRudWxsMAoGCCqBHM9VAYN1A0gAMEUC" +
                    "IEc11rh1QLonJsUK4KxcVg8YaIWqnj50yxS6KtSGJwruAiEAuc6gyCg8s+NfiZTS" +
                    "ueNqhoXGBUAWBymJM88nKBEQs/E=";


            System.out.println(" 获取加密证书x509:"+encryptCertStr);
            byte[] encryptCertBytes= Base64.decode(encryptCertStr);
            System.out.println(encryptCertBytes.length);

            if (Security.addProvider(new BouncyCastleProvider()) < 0) {
                Security.removeProvider("BC");
                if (Security.addProvider(new BouncyCastleProvider()) < 0) {
                }
            }
            ByteArrayInputStream bais=new ByteArrayInputStream(encryptCertBytes);
            CertificateFactory cf= CertificateFactory.getInstance("X.509", "BC");
            java.security.cert.X509Certificate encryptCert=(java.security.cert.X509Certificate)cf.generateCertificate(bais);
            System.out.println("序列号："+encryptCert.getSerialNumber().toString(16));
            Date date1=encryptCert.getNotBefore();
            Date date2=encryptCert.getNotAfter();

            String start = DateUtils.format(encryptCert.getNotBefore(), DateUtils.DateTimeFormatPattern.DATE_TIME_FORMAT_19_DASH.pattern);
            String end = DateUtils.format(encryptCert.getNotAfter(), DateUtils.DateTimeFormatPattern.DATE_TIME_FORMAT_19_DASH.pattern);
            System.out.println("有效期："+start+"--"+end);
            System.out.println("subjectDN："+encryptCert.getSubjectDN().toString());
            System.out.println("issuerDN："+encryptCert.getIssuerDN().toString());
            System.out.println("证书签名算法："+encryptCert.getSigAlgName());
            System.out.println("证书版本："+encryptCert.getVersion());
            // 可以进一步打印其他证书信息，例如有效期、签发者、主题等
            // ...

        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}